package com.atguigu.gulimall.gulimallelasticsearch.controller;

import com.alibaba.fastjson.JSON;
import com.atguigu.gulimall.gulimallelasticsearch.config.GulimallElasticSeachConfig;
import org.apache.http.HttpHost;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.io.IOException;
@RequestMapping("/search")
@RestController
public class SearchController {

    @Autowired
    RestHighLevelClient client;

    @RequestMapping("/index")
    public Object getIndex() throws IOException {
        IndexRequest request = new IndexRequest("users");
        request.id("1");
        User user = new User();
        String jsonString = JSON.toJSONString(user);
        request.source(jsonString, XContentType.JSON);
        IndexResponse index =client.index(request, GulimallElasticSeachConfig.COMMON_OPTIONS);

        System.out.println(index);
        client.close();

        return index;
    }

    class User{
        String name;
        Integer age;

    }

}
